local nl = require'luaNumeric'
require 'luaFDTD3D'
local plot = require "Q2DViewerEx"

----------------------------------------------------------------------------------------------------------------------------------------
--初始化参数
local MaterialView={
	funs = {};
	xAxis = 0;
	ytype = 3;
	xtype = 1;
};
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function MaterialView:new (o)
	o = o or {};
	self.__index = self;
	setmetatable(o,self)
	o:init();
	return o;
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--创建表和创建元表
function MaterialView:init ()
	self.funs = {};
	self.funs.abs = function(input) return nl.abs(input); end;
	self.funs.phs = function(input) return nl.arg(input)*180/pi; end;
	self.funs.real = function(input) return nl.real(input); end;
	self.funs.imag = function(input) return nl.imag(input); end;
	self.funs.log = function(input) 
		local  d = nl.abs(input);
		d = 20* nl.log10(d)
		return d; 
	end;
	self.fun_names = {};
	self.fun_names[0] = 'log';
	self.fun_names[1] = 'abs';
	self.ytype = 1;
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--更新数据
function MaterialView:refresh_figs ()
	self:update_parameters_from_ui();
	local pbs = Geometry.objects('probes');
	self.results={}
	self.freq = nl.new_vector();
	if #pbs >= 1 then
		for _,pb in pairs(pbs) do 
			self.freq:add( pb:freq() );
		end
	else
		self.freq = nl.LinearSpace( Geometry.Mesh.mesh_freq()/1000, Geometry.Mesh.mesh_freq(), 1000 );
	end
	self.freq:sort("");
	self.lambda = nl.new_vector();
	for k,v in pairs (self.freq) do  
		self.lambda:add( c0 / v /  Geometry.ref_units_freq() /  Geometry.ref_units_dim() );
	end 
	self.vec_eps = self.material:effective_eps( self.freq * Geometry.ref_units_freq() );
	self.vec_mu = self.material:effective_mu( self.freq * Geometry.ref_units_freq() );
	self.vec_n = nl.sqrt( self.vec_eps *  self.vec_mu );
	self.vec_impendance =   nl.sqrt( self.vec_mu / self.vec_eps );

	self:plot()
end
----------------------------------------------------------------------------------------------------------------------------------------


----------------------------------------------------------------------------------------------------------------------------------------
--画图
function MaterialView:plot ()
	self:update_parameters_from_ui();
	
	self.results={}
	if self.mattype == 0 then
		self.results[1] = {'ε1 (real part of ε)',nl.real(self.vec_eps)};
		self.results[2] = {'ε2 (imaginary part of ε)',nl.imag(self.vec_eps)};
		self.results[3] = {'μ1 (real part of μ)',nl.real(self.vec_mu)};
		self.results[4] = {'μ2 (imaginary part of μ)',nl.imag(self.vec_mu)};
	else
		self.results[1] = {'n (real part of refractive index)',nl.real(self.vec_n)};
		self.results[2] = {'k (imaginary part of refractive index)',nl.imag(self.vec_n)};
		self.results[3] = {'z (real part of impedance)',nl.real(self.vec_impendance)};
		self.results[4] = {'r (imaginary part of impedance)',nl.imag(self.vec_impendance)};
	end
	
	self.view:clearGraphs();
	local x = self.freq;
	if self.xtype ==1 then
		x = self.lambda;
	end
	local fun = self.funs[ self.fun_names[self.ytype] ];
	--assert( self.results.data['Sout'], '数据不存在！' );
	for k,v in pairs ( self.results ) do 
		self.view:addGraph(x,  fun( v[2], k ), v[1] );
	end
	self.view:rescaleAxes();
	self:set_axis();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--画图
function MaterialView:rescale ()
	self:update_parameters_from_ui();
	self:plot();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--坐标设置
function MaterialView:set_axis()
	if self.xtype ==0 then
		self.view.xAxis:setProperty( {name="频率 ["..Geometry.ref_units_freq_str()..']'} )
	else
		self.view.xAxis:setProperty( {name="波长 ["..Geometry.ref_units_dim_str()..']'} )
	end
	
	if self.ytype ==0 then
		self.view.yAxis:setProperty( {name="本构参数 [dB]"} )
	elseif self.ytype ==1 then
		self.view.yAxis:setProperty( {name="本构参数"} )
	end
	 self.view:rescaleAxes();
end
----------------------------------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------------------------------
--导出
function MaterialView:export_data (path)
	print(path);
	self.signal:save(path);
end
----------------------------------------------------------------------------------------------------------------------------------------

return MaterialView;
